package com.guoyansoft.controller;

import com.guoyansoft.config.R;
import com.guoyansoft.model.pojo.*;
import com.guoyansoft.model.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ：zyx
 * @date ：Created in 2023-04-26-17:04
 * @description：
 * @modified By：
 * @version: $
 */
@RestController
@RequestMapping("/courier")
public class CourierController {
    @Autowired
    private CourierService courierService;
    @Autowired
    private DistrictService districtService;
    @Autowired
    private ProductTeamService productTeamService;

    /**
     * @param courier 要添加的配送员实体
     * @return 响应结果
     */
    @RequestMapping("/save")
    public R save(Courier courier, Integer districtId, Integer productId) {
        courier.setDistrict(new District(districtId));
        R r2 = productTeamService.queryById(productId);
        courier.setProductTeam((ProductTeam) r2.getData());
        R r = courierService.save(courier);
        return r;
    }

    /**
     * @param id 配送员实体的编号
     * @return 删除记录数
     */
    @RequestMapping("/drop")
    public R drop(Integer[] id) {
        R r = courierService.drop(id);
        return r;
    }

    /**
     * @param courier 要修改的配送员实体
     * @return 修改记录数
     */
    @RequestMapping("/modify")
    public R modify(Courier courier, Integer districtId, Integer productId) {
        courier.setDistrict(new District(districtId));
        R r2 = productTeamService.queryById(productId);
        courier.setProductTeam((ProductTeam) r2.getData());
        R r = courierService.modify(courier);
        return r;
    }

    /**
     * @param name
     * @param curPage
     * @param pageSize    查询的所有条件
     * @return 响应结果
     */
    @RequestMapping("/queryByPage")
    public R queryByPage(String name, Integer districtId, Integer productTeamId, Integer curPage, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("name", "%"+name+"%");
        map.put("districtId", districtId);
        map.put("productTeamId", productTeamId);
        map.put("startIndex", (curPage-1)*pageSize);
        map.put("pageSize", pageSize);
        R r = courierService.queryByPage(map);
        return r;
    }
    @RequestMapping("/queryDistrict")
    public R queryDistrict() {
        R r = districtService.queryAll();
        return r;
    }

    @RequestMapping("/queryByDistrict")
    public R queryByDistrict(String[] district){
            Map<String,Object> map=new HashMap<>();

        for (int i = 0; i < district.length; i++) {
            district[i]=district[i].replace(",", "").trim();
        }

        map.put("district",district);
        for (String s : district) {
            System.out.println(s);
        }
   return   courierService.queryByDistrict(map);
    }

    @RequestMapping("/savePendingOrders")
    public R savePendingOrders(CourierAndPendingOrders courierAndPendingOrders){
        System.out.println(courierAndPendingOrders);
        R r = courierService.saveCourierAndPendingOrders(courierAndPendingOrders);
                   return r;
    }

    @RequestMapping("/login")
    public R login(Courier courier){
       return courierService.queryByLogin(courier);
    }

}
